from .celery import app
import os


@app.task
def updata_banner():
    # 在脚本中调用django项目
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "untitled15.settings")
    import django
    django.setup()
    from luffyapi.apps.home import models,serializer
    from django.core.cache import cache
    from rest_framework.mixins import ListModelMixin
    from rest_framework.viewsets import GenericViewSet
    from django.conf import settings

    class BannerView(GenericViewSet, ListModelMixin):
        # 无论有多少条待展示的数据，最多就展示3条
        queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('display_order')[:settings.BANNER_COUNTER]
        serializer_class = serializer.BannerModelSerilaizer
        def list(self, request, *args, **kwargs):
            response = super().list(request, *args, **kwargs)
            # 加到缓存
            cache.set('banner_list', response.data, 60 * 60 * 24)
